import json
import requests
import pandas as pd
import emmongo.em_mongomanager as mongoManager
from LSTM.variableset import EMVar
import time
from sysdata import sys_base_data as sysBaseData
from emmodels.em_selffund import SelfFund




def req_strategy_fund(key=None, max_count=20, sleep=1):
    time.sleep(sleep)
    if key == None:
        return None;
    headers = {
        "Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
        "Accept-Encoding": "gzip, deflate, sdch",
        "Cache-Control": "max-age=0",
        "Accept-Language": "zh-CN,zh;q=0.8,en;q=0.6",
        "Connection": "keep-alive",
        "Host": "m.iwencai.com",
        "If-Modified-Since": "Wed, 13 Jan 2016 10:41:01 GMT",
        "If-None-Match": "\"1c6001-8325-52934ce602940\"",
        "Upgrade-Insecure-Requests": "1",
        "User-Agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 8_0 like Mac OS X) AppleWebKit/600.1.3 (KHTML, like Gecko) Version/8.0 Mobile/12A4345d Safari/600.1.4"
    }

    params = {'qs': '',
              'w': key,
              'source': 'phone',
              "queryarea": "all",
              "querytype": "",
              "tid": "stockpick",
              "userid": "108444370",
              "cid": "kdp608tfek1ja6lq5sf3gmb6501462078279",
              "qid": "jjao6ntOsr4umiKBJfz5vabGdyqpQHNS",
              "perpage": "100",
              "usertype": "m"}

    r = requests.get('http://www.iwencai.com/wap/search', headers=headers, params=params, timeout=5.0)
    if r.status_code == requests.codes.ok:
        js = json.loads(r.text)
        data = (js['xuangu']['blocks'])[0]['data']['result']
        total = (js['xuangu']['blocks'])[0]['data']['total']
        if len(data)> 0:
            m = 0
            l = []
            for i in data:
                if m <= max_count:
                    l.append([i[0].replace('.SZ','').replace('.SH',''), i[1]])
                    m +=1
                else:
                    break
            df = pd.DataFrame(l, columns=[EMVar.stock_code, EMVar.stock_name])
            df['tag'] = key
            print('%s 获取成功，共有%d条' %(key, int(total)))
            return df, total
        return pd.DataFrame(), 0

def req_dfcf_fxs_stk(url=None, max_count = 20):
    # http://data.eastmoney.com/invest/invest/list.html?st=3
    # 总体排行: http://data.eastmoney.com/invest/invest/ajax.aspx?st=1&sr=-1&p=1&ps=50&js=MrhTgsnK&type=all&rt=50027461
    # 近3个月排行 http://data.eastmoney.com/invest/invest/ajax.aspx?st=3&sr=-1&p=1&ps=100&js=FLhphVeu&type=all&rt=50027419
    response = requests.get(url, timeout=5.0)
    df = pd.DataFrame()
    if response.status_code == requests.codes.ok:
        datas = json.loads(response.text.replace('var ret =',''))['data']
        l = []
        m = 0
        for item in datas:
            if int(item['CfgGs']) == 0 or int(item['CfgGs']) > 5:
                continue
            info = item['NewGgpj'].split(' ')
            if len(info) == 2 and m < max_count:
                m += 1
                stk_code, stock_name = info[0], info[1]
                l.append([stk_code, stock_name])
        df = pd.DataFrame(l, columns=[EMVar.stock_code, EMVar.stock_name])
        df['tag'] = '东方财富投顾'
        return df
    return df




def sys_all_strategy_fund():
    # 删除之前的自选
    # mongoManager.db[mongoManager.COL_SELF_FUND].delete_many({'last_update_time':EMVar.STR_NOW_YMD,'source':SelfFund.SOURCE_STRATEGY_PREDICT})
    # BBI大于5.1；BBI小于9.3；量比大于0.5小于1.1；换手率大于1%小于5%；市值从小到大
    # MTM买入信号；MACD>-0.15；涨幅小于5%；换手率小于3%；市值从小到大排列；
    df1 = None
    df2 = None
    df3 = None
    df4 = None
    df5 = None
    df6 = None
    df7 = None
    df8 = None
    df9 = None

    # df1 = req_strategy_fund(key='BBI大于5.1；BBI小于9.3；量比大于0.5 小于1.1；换手率大于1%小于5%；市值从小到大', max_count=2)
    # df2 = req_strategy_fund(key='5日的均线角度>=45.5且5日的均线角度<=90.5，上市大于100，板块资金连续流入,多头排列,DDE大单净流入',max_count=4)
    # df3 = req_strategy_fund(key='上市大于100，5日均线大于10日均线，10日均线大于20日均线，5日的均线角度>=35.5且5日的均线角度<=60.5',max_count=4)
    # df4 = req_strategy_fund(key='上升通道，上市大于100，DDE大单净流入')
    # df5 = req_strategy_fund(key='底部反转，总市值小于35亿元；无退市预警；非停牌，非涨停，总市值从小到大排名', max_count=2)
    # df6 = req_strategy_fund(key='换手率前333名，8日涨幅2%~9%，K值60以下，股价4~40，非ST，非新股，非停牌，非创业板，机构持股30%以下，市值从小到大', max_count=2)
    # df7 = req_strategy_fund(key='均线多头排列，月线macd大于0.2小于0.4，周线macd大于0.2',max_count=4)
    # df8 = req_strategy_fund(key='macd金叉的股票,月线macd大于0.2小于0.5，周线macd大于0.2',max_count=4)
    # df9 = req_strategy_fund(key='macd连续8天大于0，8天内振幅小于10%，macd大于0小于0.6，5日均线大于10日均线，10日均线大于20日均线，涨跌幅大于-2%小于2%，macd从大到小', max_count=4)

    df10 = req_dfcf_fxs_stk(url='http://data.eastmoney.com/invest/invest/ajax.aspx?st=1&sr=-1&p=1&ps=50&type=all&rt=50027461',max_count=20)
    df11 = req_dfcf_fxs_stk(url='http://data.eastmoney.com/invest/invest/ajax.aspx?st=3&sr=-1&p=1&ps=100&type=all&rt=50027419',max_count=20)
    totalDF = pd.concat([df1, df2, df3, df4, df5, df6, df7, df8, df9, df10, df11], ignore_index=True)
    # for index, item in totalDF.iterrows():
    #     sfund = sysBaseData.get_self_fund(item[EMVar.stock_code])
    #     sfund.source = SelfFund.SOURCE_STRATEGY_PREDICT
    #     sfund.tag = item['tag']
    #     sfund.saveToMongo(tb_name=mongoManager.COL_SELF_FUND)
    return totalDF

# req_dfcf_fxs_stk()
# sys_all_strategy_fund()